<?php


namespace App\Api;
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,PATCH');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

use App\Domain\CartDetail as CartDetailDomain;
use PhalApi\Api;

class CartDetail extends Api
{
    public function getRules()
    {
        return array(
            'getCartDetailInfoByCid' => array(
                'cart_id' => array('name' => 'cart_id', 'require' => true, 'min' => 1, 'desc' => '创建购物车的id')
            ),
            'insertCartDetailInfo' => array(
                'cart_id' => array('name' => 'cart_id', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '购物车的id'),
                'good_id' => array('name' => 'good_id', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的id'),
                'good_num' => array('name' => 'good_num', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的添加数量，默认传值1'),
                'good_money' => array('name' => 'good_money', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的价格'),
            ),
            'getCartDetailInfoByCidAndGid' => array(
                'cart_id' => array('name' => 'cart_id', 'require' => true, 'min' => 1, 'desc' => '购物车的id'),
                'good_id' => array('name' => 'good_id', 'require' => true, 'min' => 1, 'desc' => '点击添加到购物车的商品id')

            ),
            'updateCartDetailInfoById' => array(
                'cartDetail_id' => array('name' => 'cartDetail_id', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '购物车详细列表的id'),
                'good_num' => array('name' => 'good_num', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的数量'),
                'good_money' => array('name' => 'good_money', 'require' => true, 'min' => 1,  'source' => 'post', 'desc' => '商品的价格'),
            ),
            'deleteCartDetailInfoByCid'=>array(
                'cart_id' => array('name' => 'cart_id', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '购物车的id'),

            ),
        );
    }

    /**
     * 根据购物车id查看购物车详情的接口，购物车id与用户id匹配的
     * @desc 根据购物车id(cart_id)，获取这辆购物车里所有的信息
     */
    public function getCartDetailInfoByCid()
    {
        $cart_id = $_GET['cart_id'];
        $domain = new CartDetailDomain();
        $info = $domain->getCartDetailInfoByCid($cart_id);
        return array('info' => $info);
    }

    /**
     * 插入购物车信息
     * @desc 不存在购物车id与货物id相匹配记录，则插入新参数
     * 返回新增的id
     */
    public function insertCartDetailInfo()
    {
        $cart_id = $_POST['cart_id'];
        $good_id = $_POST['good_id'];
        $good_num = $_POST['good_num'];
        $good_money = $_POST['good_money'];
        $domain = new CartDetailDomain();
        $id = $domain->insertCartDetailInfo($cart_id, $good_id, $good_num, $good_money);
        return array('id' => $id);

    }

    /**
     * 查询是否存在已经添加过指定用户指定商品
     * @desc 在添加到购物车之前进行此查询判断，如果存在记录则调用修改接口修改数量与小计，若不存在则调用创建接口
     * 返回一组数据，正常为一条或空
     */
    public function getCartDetailInfoByCidAndGid(){
        $cart_id = $_GET['cart_id'];
        $good_id = $_GET['good_id'];
        $domain = new CartDetailDomain();
        $info = $domain->getCartDetailInfoByCidAndGid($cart_id,$good_id);
        return array('info' => $info);
    }

    /**
     * 更新已经存在用户添加过的商品购物车详细信息
     * @desc 在查询后如果存在已添加的记录，则执行此接口进行更改传值操作
     * 返回 int(1)，表示 正常影响的行数，int(0)，表示 无更新，或者数据没变化，boolean(false)，表示 更新异常、失败
     */
    public function updateCartDetailInfoById(){
        $cartDetail_id = $_POST['cartDetail_id'];
        $good_num = $_POST['good_num'];
        $good_money = $_POST['good_money'];
        $domain = new CartDetailDomain();
        $info = $domain->updateCartDetailInfoById($cartDetail_id,$good_num,$good_money);
        return array('info' => $info);

    }

    /**
     * 清空购物车接口
     * @desc 使用购物车id，删除同一个购物车的所有信息
     * 按条件进行删除，并返回影响的行数
     */
    public function deleteCartDetailInfoByCid()
    {
        $cart_id = $_POST['cart_id'];
        $domain = new CartDetailDomain();
        $row = $domain->deleteCartDetailInfoByCid($cart_id);
        return array('row' => $row);
    }
}